/*
 IMMS: Intelligent Multimedia Management System
 Copyright (C) 2001-2009 Michael Grigoriev

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include <iostream>

#include <immsutil.h>

#include "fftprovider.h"

using std::cerr;
using std::endl;

FFTWisdom::FFTWisdom() : shouldexport(true)
{
    // Grab FFTW wisdom, if available, on instantiation. 
    // FFTW handles the storage and usage internally, which gives us the
    // benefit for all fftw functions even if we don't explicitly do anything
    // with this variable or class. See the fftw docs - "wisdom .. is stored
    // in a global, private data structure managed internally by FFTW";
    // http://www.fftw.org/fftw3_doc/Words-of-Wisdom_002dSaving-Plans.html#Words-of-Wisdom_002dSaving-Plans

    FILE *wisdom = fopen(get_imms_root(".fftw_wisdom").c_str(), "r");
    if (wisdom)
    {
        shouldexport = !fftw_import_wisdom_from_file(wisdom);
        fclose(wisdom);
    }
    else
        cerr << "analyzer: Growing wiser. This may take a while." << endl;
}


FFTWisdom::~FFTWisdom()
{
    if (!shouldexport)
        return;

    FILE *wisdom = fopen(get_imms_root(".fftw_wisdom").c_str(), "w");
    if (wisdom)
    {
        fftw_export_wisdom_to_file(wisdom);
        fclose(wisdom);
    }
    else
        cerr << "analyzer: Could not write to wisdom file!" << endl;
}
